package com.tarena.rocketmq.consumers;

import com.tarena.rocketmq.service.HelloService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 根据监听的主题 获取消息后 底层Listener调用的对象
 */
@Component
@RocketMQMessageListener(
        topic="topic01",
        consumerGroup = "test-consumer-group02"
        //selectorExpression = "*",
        //messageModel = MessageModel.BROADCASTING
)
public class MyConsumerListener02 implements RocketMQListener<String> {
    @Autowired
    private HelloService helloService;
    /**
     * 核心消费逻辑 结合业务实现的方法
     * @param name 经过底层监听器 反序列化的 body消息数据
     */
    @Override
    public void onMessage(String name) {
        System.out.println("消费者02拿到消息,执行业务:"+name);
        //消费逻辑
        String result =null;
        try{
            result = helloService.sendMsg(name);
        }catch (RuntimeException e){
            //假设runtime是业务异常 自己记录日志 不会向外抛出,一旦向外抛出,消息会被重新消费一次
            System.out.println("业务执行失败,入参 出参");
        }
        //onMessage是底层监听器调用的,但是底层监听器方法consumeMessage 有返回值 CONSUME_SUCCESS RECONSUME_LATER
    }
}
